<?
    // Initialize the session.
    session_start();
    require_once("../inc/checkAdminPagePermissions.php");
    require_once("../inc/config.inc.php");
    require_once('../conexion.php');
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">

<html>
<head>
    <title><? echo _SITE_NAME; ?> :: Panel Administrativo :: Inicio</title>
    <meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
    <link href="../css/main.css" type=text/css rel=stylesheet>
	<script>
function cerrar()
{
if (confirm("Desea salir sin aplicar los cambios ?"))
   {
    document.location="users.php";
   }
}
function validar(forma)
{
if (forma.usuario.value=="")
{
 alert("Por favor introduzca su nombre");
 forma.usuario.focus();
 return false;
}
if (forma.login.value=="")
{
 alert("Por favor introduzca su login");
 forma.login.focus();
 return false;
}
if (forma.clave.value=="")
{
 alert("Por favor introduzca su clave");
 forma.clave.focus();
 return false;
}
if (forma.clave2.value=="")
{
 alert("Por favor introduzca su confirmacion de clave");
 forma.clave2.focus();
 return false;
}
if (forma.clave.value!=forma.clave2.value)
{
 alert("Ambas claves deben ser iguales");
 forma.clave.focus();
 return false;
}


return true;
}
function Ir() { 
  document.location="users.php";
  
}

	</script>
</head>


<!-- BEGIN MAIN CONTENT ARE -->
<body style="background: #ffffff;">

<div align="left">
  <h4>Seguridad -> Usuarios -> Insertar Usuario</h4>
</div>
<?php

$niveles="SELECT * FROM empresas";
$datosnivel=mysql_query($niveles);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
		$usuario=$_POST['usuario'];
        $nivel=$_POST['nivel'];
		$empresa=$_POST['empresa'];
		$login=$_POST['login'];
		$clave=$_POST['clave'];
		
  $insertSQL = "INSERT INTO admins (last_name, username,password, nivel,idempresa) VALUES ('$usuario','$login','$clave','$nivel','$empresa')";
  mysql_query($insertSQL); 
  
  ?>
  <script>document.location='users.php';</script>
<? }


?>
<form action="insertarusuario.php" method="post" enctype="multipart/form-data" name="form1" id="form1" onSubmit="return validar(this)">
  <table width="95%" border="0" align="center" cellpadding="1" cellspacing="1">
  <tr>
  <td colspan="2"><div align="left">(*) Campos Obligatorios 
  </div></td>
  </tr>
    <tr >
      <td colspan="2" ><hr size="1" /></td>
    </tr>
    <tr  >
      <td width="15%" ><div align="left"><strong>Usuario:</strong></div></td>
      <td width="85%" ><div align="left">
        <input name="usuario" type="text" id="usuario" size="50" maxlength="250" title="Nombre del usuario" >
      (*)</div></td>
    </tr>
	<tr  >
      <td width="15%" ><div align="left"><strong>Login:</strong></div></td>
      <td width="85%" ><div align="left">
        <input name="login" type="text" id="login" size="50" maxlength="250" title="Login del usuario" >
      (*)</div></td>
    </tr>
	<tr  >
      <td width="15%" ><div align="left"><strong>Clave:</strong></div></td>
      <td width="85%" ><div align="left">
        <input name="clave" type="password" id="clave" size="50" maxlength="250" title="Clave del usuario" >
      (*)</div></td>
    </tr>
	<tr  >
      <td width="15%" ><div align="left"><strong>Confirme clave:</strong></div></td>
      <td width="85%" ><div align="left">
        <input name="clave2" type="password" id="clave2" size="50" maxlength="250" title="Confirmar Clave del usuario" >
      (*)</div></td>
    </tr>
	<tr  >
      <td width="15%" ><div align="left"><strong>Empresa:</strong></div></td>
      <td width="85%" ><div align="left">
        <select name="empresa" id="empresa" title="Empresa">
          <? while ($fila=mysql_fetch_assoc($datosnivel)) { ?>
          <option value="<?=$fila["id"]; ?>"><?=$fila["razon_social"]; ?></option>

		  <? } ?>
        </select>
      </div></td>
    </tr>
	<tr  >
      <td width="15%" ><div align="left"><strong>Nivel:</strong></div></td>
      <td width="85%" ><div align="left">
        <select name="nivel" id="nivel" title="Nivel de Acceso">
          <? if ($fila["nivel"]!="") {?>
          <OPTION value="<?=$fila["nivel"]; ?>" selected="selected">
            <?=$fila["nivel"]; ?>
            </OPTION>
          <? } ?>
          <option value="Usuario">Usuario</option>
          <option value="Administrador">Administrador</option>
        </select>
      </div></td>
    </tr>

    <tr >
      <td colspan="3" ><hr size="1" /></td>
    </tr>
    <tr >
      <td  colspan="3" align="center" ><input name="Submit" type="submit"  value="Aceptar Datos" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="Submit3" type="button"  onClick="cerrar()" value="Menu Usuarios" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
 
</form>
</body>
</html>
